(function ($app) {
    $app.module('gtPartyApp').controller('pf_djscgzEditCtrl', function ($scope, $http, $modal, $ocLazyLoad, $alert, $loading, $state, $bsRouterState, AuthUser, $timeout, $excel, $partySelect, $filter) {
        $scope.editParams = $bsRouterState.$params($scope);
        $scope.pageId = $bsRouterState.tabs.activeTab;
        $scope.dataModel = {};
        $scope.pxjbTypeList = [];
        $scope.pxlbTypeList = [];

        $scope.reqFileModel = {
            selectdata: {FileRefID: '', pageindex: 1, pagesize: 10, ptotal: 0},//查询参数：FileRefID为文件关联ID
            filetype: 71,//文件类型
            items: [],//列表数据地址
            readonly: false,//是否只能查看
            title: '附件',
            addFileButton: '上传'
        };

        //选择党支部
        $scope.partyConfig = {
            scope: $scope,
            selectedKey: '',
            selectedText: '',
            selectedData: [],
            isMulti: false,
            isshowxz: 1
        };
        $partySelect.initServer($scope.partyConfig);
        $scope.$watch("partyConfig.selectedKey", function (newVal, oldVal) {
            if (newVal) {
                $scope.dataModel.dzzdm = $scope.partyConfig.selectedKey;
                $scope.dataModel.dzzmc = $scope.partyConfig.selectedText;

                $scope.partyConfig.selectedText = "";
                $scope.partyConfig.selectedKey = "";
            }
        }, true);
        $scope.selectParty = function () {
            $partySelect.showModal();
        };

        $scope.loadPxjbTypes=function(){

            $http({
                method: 'get',
                url: '../../api/dictionary/getDictionaryListByDicTypeKey',
                params: {dicTypeKey: 'pxjbType'}
            }).then(function (result) {
                $scope.pxjbTypeList = result.data;
            }, function (resp) {

            });
        };

        $scope.loadPxlbTypes=function(){

            $http({
                method: 'get',
                url: '../../api/dictionary/getDictionaryListByDicTypeKey',
                params: {dicTypeKey: 'pxlbType'}
            }).then(function (result) {
                $scope.pxlbTypeList = result.data;
            }, function (resp) {

            });
        };

        $scope.getData = function () {
            $http
            ({
                method: 'get', url: "../../api/partylife/djscgz/get",
                params: {
                    'id': $scope.editParams.id
                }
            }).then(function (result) {
                $scope.dataModel = result.data.item;
                $scope.reqFileModel.selectdata.FileRefID = $scope.dataModel.id;
                if(!$scope.dataModel.dzzdm){
                    $scope.dataModel.dzzdm = AuthUser.getUser().DPCode;
                    $scope.dataModel.dzzmc = AuthUser.getUser().DPName;
                }
                $scope.loadPxjbTypes();
                $scope.loadPxlbTypes();
            }, function (resp) {
                $scope.showMsg('错误', '服务器错误：' + resp.data);
            });
        };

        $scope.save = function (isflag) {
            if (isflag) {

                $loading.show();
                $http({
                    method: "post",
                    url: "../../api/partylife/djscgz/save",
                    data: $scope.dataModel
                }).then(function (result) {
                    $loading.hide();
                    if (result.data.success) {
                        $scope.showMsg('成功', "操作成功");
                        $scope.pagechange();
                        //重新刷新列表
                        $scope.$emit("tabReloadData", {name: 'pf_djscgzListCtrl', data: 1});
                    } else {
                        $scope.showMsg('失败', result.data.msg);
                    }
                }, function (resp) {
                    $loading.hide();
                    $scope.showMsg('错误', '服务器错误：' + resp.data);
                });
            }
        };

        $scope.pagechange = function () {
            $bsRouterState.$closeTab($scope);
        };

        $scope.getData();
    });
})(angular);